public class BM1 {

    private static final int MOD = 1000000007;

    public static int InversePairs(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        return (int)(mergeSort(nums, 0, nums.length - 1) % MOD);
    }

    private static long mergeSort(int[] nums, int left, int right) {
        if (left >= right) {
            return 0;
        }
        int mid = left + (right - left) / 2;
        long count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right);
        count %= MOD;
        count += merge(nums, left, mid, right);
        return count % MOD;
    }

    private static long merge(int[] nums, int left, int mid, int right) {
        int[] temp = new int[right - left + 1];
        int i = left, j = mid + 1, k = 0;
        long count = 0;

        while (i <= mid && j <= right) {
            if (nums[i] <= nums[j]) {
                temp[k++] = nums[i++];
            } else {
                temp[k++] = nums[j++];
                count += (mid - i + 1);
                count %= MOD;
            }
        }

        while (i <= mid) {
            temp[k++] = nums[i++];
        }

        while (j <= right) {
            temp[k++] = nums[j++];
        }

        for (i = 0; i < temp.length; i++) {
            nums[left + i] = temp[i];
        }

        return count;
    }
}
